package com.ruoyi.hr.service.impl.welfare.welfarebaseadjustment;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.welfare.welfareInfo.WelfareInfoMapper;
import com.ruoyi.hr.mapper.welfare.welfarebaseadjustment.SocialSecurityBaseAdjustmentMapper;
import com.ruoyi.hr.mapper.welfare.welfarechange.WelfareChangeMapper;
import com.ruoyi.hr.service.welfare.welfarebaseadjustment.SocialSecurityBaseAdjustmentService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SocialSecurityBaseAdjustmentImpl implements SocialSecurityBaseAdjustmentService {

    @Autowired
    SocialSecurityBaseAdjustmentMapper socialSecurityBaseAdjustmentMapper;

    @Autowired
    WelfareChangeMapper welfareChangeMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    WelfareInfoMapper welfareInfoMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult addbeneChangeSocialAdd(String allPeid) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allPeid.split(",");
            String msg;//异常信息
            for (String s : array) {
                //根据ID取出人名
                Map<String, Object> map = welfareInfoMapper.findNameByPeid(s);
                sql = "declare @RetVal varchar(50) exec cSP_BeneChangeSocialAdd " + s + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = socialSecurityBaseAdjustmentMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += map.get("name") + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData(Map<String, Object> map,List<Authority> authorities,int isAdmin) {
        List<String> allIdList = new ArrayList<String>();
        if (!"".equals(map.get("allId"))) {
            String[] array = map.get("allId").toString().split(",");
            for (String id : array) {
                allIdList.add(id);
            }
        }
        map.put("allIdList", allIdList);
        return socialSecurityBaseAdjustmentMapper.findData(map,authorities,isAdmin);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public Map<String, Object> findDataById(String id) {
        return socialSecurityBaseAdjustmentMapper.findDataById(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult procedure(String allId, String procedure) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String s : array) {
                //根据id取出人名
                Map<String, Object> map = socialSecurityBaseAdjustmentMapper.findNameById(s);
                if("cSP_BeneChangeDec".equals(procedure)){
                    sql = "declare @RetVal varchar(50) exec " + procedure + " " + s + ",@RetVal output select @RetVal";
                }else{
                    sql = "declare @RetVal varchar(50) exec " + procedure + " " + s + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                }
                String retVal = socialSecurityBaseAdjustmentMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += map.get("name") + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateSocialSecurityBase(Map map) {
        //有就insert，没有就update
        if (socialSecurityBaseAdjustmentMapper.findCountXvalue(map) == 0) {
            socialSecurityBaseAdjustmentMapper.insertXvalue(map);
        } else {
            socialSecurityBaseAdjustmentMapper.updateXvalue(map);
        }
        return socialSecurityBaseAdjustmentMapper.updateSocialSecurityBase(map);
    }
}
